{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/anaconda/envs/py35/lib/python3.5/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from google.protobuf.json_format import MessageToJson\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "path_to_tfrecord = '/datadrive/emammal_tfrecords/eMammal_loc_splits_20180929/train-00104-of-00110'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n",
      "Found example\n",
      "['2']\n",
      "{}\n",
      "{}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "i = 0\n",
    "example = ''\n",
    "\n",
    "# note that the 'image/class/label' is at the image level - look at the bbox level instead\n",
    "for example in tf.python_io.tf_record_iterator(path_to_tfrecord):\n",
    "    \n",
    "    result = tf.train.Example.FromString(example)\n",
    "    jsonMessage = MessageToJson(tf.train.Example.FromString(example))\n",
    "\n",
    "    jsonExample = json.loads(jsonMessage)\n",
    "    if 'value' not in jsonExample['features']['feature']['image/object/class/label']['int64List']:\n",
    "        print(jsonExample['features']['feature']['image/object/class/label']['int64List'])\n",
    "    \n",
    "    elif jsonExample['features']['feature']['image/object/class/label']['int64List']['value'][0] == '2':\n",
    "        print('Found example')\n",
    "        print(jsonExample['features']['feature']['image/object/class/label']['int64List']['value'])\n",
    "    \n",
    "    i += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "color_space = jsonExample['features']['feature']['image/colorspace']['bytesList']['value'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'UkdC'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "color_space"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Upload to blob storage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from azure.storage.blob import BlobServiceClient\n",
    "import json\n",
    "import os\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.environ[\"AZ_STORAGE_KEY\"] = ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "key = os.environ[\"AZ_STORAGE_KEY\"]\n",
    "blob_service = BlobServiceClient(account_url='emammaltrain.blob.core.windows.net', credential=key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 141/141 [25:44<00:00, 10.95s/it]\n"
     ]
    }
   ],
   "source": [
    "tfrecords_dir = '/datadrive/emammal_tfrecords/eMammal_loc_splits_20180929'\n",
    "container_name = 'tf20180929'\n",
    "\n",
    "container_client = blob_service.get_container_client(container_name)\n",
    "for tfrecord in tqdm(os.listdir(tfrecords_dir)):\n",
    "    if not tfrecord.startswith('failed'):\n",
    "        local_path = os.path.join(tfrecords_dir, tfrecord)\n",
    "        with open(local_path, 'rb') as f:\n",
    "            container_client.upload_blob(name=tfrecord, data=f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:py35]",
   "language": "python",
   "name": "conda-env-py35-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
